package com.example.utils;

import android.graphics.Canvas;

import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Path;

import com.example.bean.Mystyle;
import com.example.bean.Point;
import com.example.myview.ChartConstant;
import com.example.myview.StyleView;


/**
 * 画样式工具
 */
public class DrawStyleUtil {
    public static int yuan_banjin=30;
    public static int color=0xff4385F3;
    public static int width=2;
    /**
     * 0 画实线  大于0画虚线
     */
    public static int xuxian_width=0;
    /**
     *  点击之后画圆
     * @param canvas
     * @param point 原点坐标
     * @param
     */
    public   void drawPoint(Canvas canvas, com.example.bean.Point point,Mystyle style)
    {
        //设置画笔
        Paint paint=new Paint();
        paint.setAntiAlias(true);
        paint.setColor(style.getColor());
        paint.setAlpha(40);
        paint.setStyle(Paint.Style.FILL_AND_STROKE);

        //画圆
        canvas.drawCircle(point.getX(),point.getY(),yuan_banjin,paint);

        //设置画笔
        Paint paint2=new Paint();
        paint2.setAntiAlias(true);
        paint2.setColor(style.getColor());
        paint2.setStyle(Paint.Style.FILL_AND_STROKE);

        //画圆
        canvas.drawCircle(point.getX(),point.getY(),8,paint2);
    }
    /**
     *  点击之后画圆
     * @param canvas
     * @param point 原点坐标
     * @param
     */
    public   void drawLine(Canvas canvas, Point point, Point point2, Mystyle style)
    {
        //设置画笔
        Paint paint=new Paint();
        paint.setAntiAlias(true);
        paint.setColor(style.getColor());
        paint.setStyle(Paint.Style.STROKE);
       paint.setStrokeWidth(style.getLine_width());
       if(style.getXuxian_width()>0)
       {
           DashPathEffect dp=new DashPathEffect(new float[] {style.getXuxian_width(),style.getXuxian_width()}, 0);
           paint.setPathEffect(dp);
       }


        //画圆

        //canvas.drawLine(point.getX(),point.getY(),point2.getX(),point2.getY(),paint);

        Path mPath= new Path();

        mPath.reset();
        mPath.moveTo(point.getX(), point.getY());
        mPath.lineTo(point2.getX(), point2.getY());
        canvas.drawPath(mPath,paint);
    }


    /**
     * 写时间，并且带框
     */
    public void drawTimeTextWithRect(Canvas canvas, float x,float y,float width,String time,Mystyle style) {

        //设置画笔
        Paint p=new Paint();
        p.setAntiAlias(true);
        p.setColor(0xffffffff);
        p.setStrokeWidth(2);
        p.setStyle(Paint.Style.FILL);
        p.setTextSize(32f);
        float textWidth = LineUtil.getTextWidth(p, time) + 20;

        Paint rp = new Paint();
        rp.setColor(style.getColor());
        rp.setStyle(Paint.Style.FILL);
        rp.setStrokeWidth(2f);
        //1,先画白底
        float startX = x - textWidth / 2;
        float endX = x + textWidth / 2;
        if(startX < 0) {
            startX = 2f;
            endX = startX + textWidth;
        }
        if(endX > width) {
            endX = width - 2;
            startX = endX - textWidth;
        }
        canvas.drawRect(startX, y + 2, endX, y + 30, rp);
        rp.setColor(ColorUtil.COLOR_SHIZHI_TIME);
        rp.setStyle(Paint.Style.STROKE);
        //2，再画黑框
        canvas.drawRect(startX, y + 2, endX, y + 30, rp);
        //3，写文字
        canvas.drawText(time, startX + 10, y + 27.5f, p);
    }

    /**
     * 写文字，并且为文字带上背景，等于在文字后方画上一个Rect
     */
    public void drawPriceTextWithRect(Canvas canvas, float x, float y,float height ,float width ,String text,Mystyle style) {
        //设置画笔
        Paint p=new Paint();
        p.setAntiAlias(true);
        p.setColor(0xffffffff);
        p.setStrokeWidth(2);
        p.setStyle(Paint.Style.FILL);
        p.setTextSize(32f);

        float textWidth = LineUtil.getTextWidth(p, text) + 10;
        Paint rp = new Paint();
        rp.setColor(style.getColor());
        rp.setStyle(Paint.Style.FILL);
        rp.setStrokeWidth(2f);
        float startY = y - 15f;
        float endY = y + 15f;
        if(startY < 0) {
            startY = 0f;
            endY = startY + 30f;
        } else if(endY > height) {
            endY = height;
            startY = endY - 30f;
        }

        if (x < 100) {
            //X轴在左侧，该框画在右侧
            //1,先画白底
            canvas.drawRect(width - textWidth, startY, width, endY, rp);
            rp.setColor(ColorUtil.COLOR_SHIZHI_TIME);
            rp.setStyle(Paint.Style.STROKE);
            //2，再画黑框
            canvas.drawRect(width - textWidth, startY, width, endY, rp);
            p.setTextAlign(Paint.Align.RIGHT);
            canvas.drawText(text, width - 5f, endY - 3, p);
        } else {
            //X轴在右侧，改框画左侧
            canvas.drawRect(0, startY, textWidth, endY, rp);
            rp.setColor(Color.BLACK);
            rp.setStyle(Paint.Style.STROKE);
            canvas.drawRect(0, startY, textWidth, endY, rp);
            p.setTextAlign(Paint.Align.LEFT);
            canvas.drawText(text, 5f, endY - 3, p);
        }
    }


    public static int getColor() {
        return color;
    }

    public static void setColor(int color) {
        DrawStyleUtil.color = color;
    }

    public static int getWidth() {
        return width;
    }

    public static void setWidth(int width) {
        DrawStyleUtil.width = width;

    }

    public static int getXuxian_width() {
        return xuxian_width;
    }

    public static void setXuxian_width(int xuxian_width) {
        DrawStyleUtil.xuxian_width = xuxian_width;
    }
}
